package com.labmi.experiment;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EntityResult;
import javax.persistence.FieldResult;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.SqlResultSetMapping;


@NamedNativeQueries(
{
@NamedNativeQuery(	name="singleCategoryFullPath" , 
		query="SELECT root.id   AS root_id, " +
"		root.name AS root_name, " +
"		secondCategory.id AS secondCategory_id, " +
"		secondCategory.name AS secondCategory_name, " +
"		thirdCategory.id  AS thirdCategory_id, 	thirdCategory.name 	AS thirdCategory_name " +
" FROM category AS root" +
" LEFT OUTER JOIN category AS secondCategory ON secondCategory.parent = root.id " +
" LEFT OUTER JOIN category AS thirdCategory ON thirdCategory.parent = secondCategory.id " +
" WHERE root.parent =0 AND (thirdCategory.id = ?1 or secondCategory.id = ?1)" 
	),
	
	@NamedNativeQuery(	name="calculateRootCategory" , 
		query="SELECT DISTINCT root.id   AS root_id, " +
		"		root.name AS root_name, " +
		"		secondCategory.id AS secondCategory_id, " +
		"		secondCategory.name AS secondCategory_name, " +
		"		thirdCategory.id  AS thirdCategory_id, 	thirdCategory.name 	AS thirdCategory_name " +
		" FROM category AS root" +
		" LEFT OUTER JOIN category AS secondCategory ON secondCategory.parent = root.id " +
		" LEFT OUTER JOIN category AS thirdCategory ON thirdCategory.parent = secondCategory.id " +
		" WHERE root.parent =0 AND (root.id = ?1 OR  secondCategory.id = ?1 OR thirdCategory.id = ?1)"
	)		
	})

@SqlResultSetMapping(
	    name="categoryListMapping",
	    entities = {
	        @EntityResult(
	            entityClass=CategoriesMappingBean.class,
	            fields = {
	                @FieldResult(name = "rootId", 				column = "root_id"),
	                @FieldResult(name = "rootName", 			column = "root_name"),
	                @FieldResult(name = "secondCategoryId", 	column = "secondCategory_id"),
	                @FieldResult(name = "secondCategoryName", 	column = "secondCategory_name"),
	                @FieldResult(name = "thirdCategoryId",		column = "thirdCategory_id"),
	                @FieldResult(name = "thirdCategoryName",	column = "thirdCategory_name"),
	            }
	        )
	    }
	 )

@Entity
public class Category {

	@Id
	private int id;
	@OneToMany(mappedBy="category", orphanRemoval=false )
	private List<ClothingItem> itemList;
	
	private String name;

	private int parent;
	
	private int itemCategoryCount;
	
	private String description;
	
	
	public Category() {
		
	}
	
	public void addItem(ClothingItem item) {
		if(item != null)
		{
			if(itemList == null)
				itemList = new ArrayList<ClothingItem>();
			
			itemList.add(item);
			item.setCategory(this);
		}
			
	}
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public int getParent() {
		return parent;
	}

	public void setParent(int parent) {
		this.parent = parent;
	}

	public int getItemCategoryCount() {
		return itemCategoryCount;
	}

	public void setItemCategoryCount(int itemCategoryCount) {
		this.itemCategoryCount = itemCategoryCount;
	}

	public void setItemList(List<ClothingItem> itemList) {
		this.itemList = itemList;
	}

	public List<ClothingItem> getItemList() {
		return itemList;
	}

	
}
